from __future__ import absolute_import
from lxml import etree
import os
import datetime
import logging
logger = logging.getLogger('pkgr.repository.metadata')
logger.setLevel(logging.INFO)

class RepositoryMetadataData(object):
    def __init__(self):
        pass
    
class RepositoryMetadata(object):
    """ Contains everything needed to work a repo md.
    
        An example repo md:
    
            <?xml version="1.0" encoding="UTF-8"?>
            <repomd xmlns="http://linux.duke.edu/metadata/repo" xmlns:rpm="http://linux.duke.edu/metadata/rpm">
                <revision>1337941209</revision>
                <tags>
                    <content>binary-x86_64</content>
                    <distro cpeid="cpe:/o:fedoraproject:fedora:17">r</distro>
                </tags>
                <data type="group">
                    <checksum type="sha256">42ec1be1745e71753d57892fefff57fc16d98541662637e1a8f2c0b2f18870f6</checksum>
                    <location href="repodata/42ec1be1745e71753d57892fefff57fc16d98541662637e1a8f2c0b2f18870f6-comps
                    -f17.xml"/>
                    <timestamp>1337943054.3</timestamp>
                    <size>1958151</size>
                </data>
                <data type="filelists">
                    <checksum type="sha256">a9c70025ee9577048b4aea69e1d10cede198546ab342bd508edb6e995d5908d4</checksum>
                    <open-checksum type="sha256">e49b1f6ecac755a8ae0a5e919d9e1dbbd0fa2910cbef32d82e4e8eb82871252b
                    </open-checksum>
                    <location href="repodata/a9c70025ee9577048b4aea69e1d10cede198546ab342bd508edb6e995d5908d4
                    -filelists.xml.gz"/>
                    <timestamp>1337941751</timestamp>
                    <size>21599675</size>
                    <open-size>290745531</open-size>
                </data>
                ...
            </repomd>
    """
    def __init__(self):
        pass
    
    def load(self, f):
        self.parse(f.read())
        
    def load_file(self, filename):
        self.filename = os.path.abspath(filename)
        self.dirname = os.path.dirname(self.filename)
        self.parse(open(filename).read())
        
    def parse(self, data):
        """ Loads and parses a repomd to memory. """
        logger.debug('Parsing %s...', len(data))
        
        root = etree.XML(data)
        
        self.data_index = {}
        
        for element in root:
            if element.tag != '{http://linux.duke.edu/metadata/repo}data':
                continue
            
            location_el = element.find('.//{http://linux.duke.edu/metadata/repo}location')
            # checksum after compression
            checksum_el = element.find('.//{http://linux.duke.edu/metadata/repo}checksum')
            
            data = RepositoryMetadataData()
            data.type = element.get('type')
            data.checksum = checksum_el.text 
            data.checksum_type = checksum_el.get('type')
            data.location_href = location_el.get('href')
            
            timestamp = float(element.find('.//{http://linux.duke.edu/metadata/repo}timestamp').text)
            data.timestamp = datetime.datetime.fromtimestamp(timestamp)
            
            size_el = element.find('.//{http://linux.duke.edu/metadata/repo}size')
            if size_el is not None:
                data.size = int(size_el.text)
            
            self.data_index[data.type] = data
     
    def get_data_file(self, type):
        """ """
        return self.data_index.get(type).location_href
             
    def get_data(self, type):
        """ """
        return self.data_index.get(type)
        
    def __str__(self):
        pass
